package com.vxdata.utils.modules.fileListener.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vxdata.common.utils.file.FileUtils;
import com.vxdata.utils.modules.fileListener.entity.FileBase64;
import com.vxdata.utils.modules.fileListener.mapper.FileBase64Mapper;
import com.vxdata.utils.modules.fileListener.service.IFileBase64Service;
import com.vxdata.utils.modules.fileListener.utils.ConcurrentHashMapUtil;
import com.vxdata.utils.modules.fileListener.utils.FileToBase64Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sunhongyun
 * @since 2023-09-25
 */
@Service
public class FileBase64ServiceImpl extends ServiceImpl<FileBase64Mapper, FileBase64> implements IFileBase64Service {
    @Value("${base64.type}")
    private String type;

    public void createFile(File file) {
        Object o = ConcurrentHashMapUtil.get(file.getName() + FILE_OPERATE.CEEATE);
        if (o != null) {
            return;
        }
        String base64 = FileToBase64Util.fileToBase64(file);
        FileBase64 f = new FileBase64();
        f.setType(type);
        f.setOperate(FILE_OPERATE.CEEATE);
        f.setName(file.getName());
        f.setPath(file.getAbsolutePath());
        f.setBase64(base64);
        f.setCreateDate(LocalDateTime.now());
        save(f);
    }


    public void deleteFile(File file) {
        Object o = ConcurrentHashMapUtil.get(file.getName() + FILE_OPERATE.DELETE);
        if (o != null) {
            return;
        }
        FileBase64 f = new FileBase64();
        f.setType(type);
        f.setOperate(FILE_OPERATE.DELETE);
        f.setName(file.getName());
        f.setPath(file.getAbsolutePath());
        f.setCreateDate(LocalDateTime.now());
        save(f);
    }


    public void monitorBase64Sql() {
        log.debug("bsae-file 定时任务开始执行");
        List<FileBase64> base64List = lambdaQuery()
                .ne(FileBase64::getType, type)
                .le(FileBase64::getCreateDate, LocalDateTime.now()).list();
        base64List.forEach(b -> {
            // 两分钟缓存
            if (b.getOperate().equals(FILE_OPERATE.CEEATE)) {
                createFile(b);
            } else {
                deleteFile(b);
            }
        });
    }

    public void createFile(FileBase64 b) {
        ConcurrentHashMapUtil.put(b.getName() + FILE_OPERATE.CEEATE, b, ConcurrentHashMapUtil.CACHE_HOLD_TIME_2M);
        FileToBase64Util.base64ToFile(b.getPath(), b.getBase64());
        lambdaUpdate().eq(FileBase64::getId, b.getId()).remove();
    }

    public void deleteFile(FileBase64 b) {
        ConcurrentHashMapUtil.put(b.getName() + FILE_OPERATE.DELETE, b, ConcurrentHashMapUtil.CACHE_HOLD_TIME_2M);
        FileUtils.deleteFile(b.getPath());
        lambdaUpdate().eq(FileBase64::getId, b.getId()).remove();
    }


    public static class FILE_OPERATE {
        public static final String CEEATE = "0";
        public static final String DELETE = "1";
    }


}
